<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2018/4/22
 * Time: 下午5:36
 */

namespace backend\server;


use common\logic\RentingTypeImageLogic;
use common\models\RentingTypeImageModel;
use common\server\Server;

class RentingTypeImageServer extends Server
{

    public function save(RentingTypeImageModel $model){
        $this->saveFile($model);
        return RentingTypeImageLogic::instance()->save($model);
    }

    /**
     * 保存上传文件banner、广告位
     * @param \common\models\RentingTypeImageModel $model
     */
    protected function saveFile(RentingTypeImageModel $model){
        if ($_FILES && !empty($_FILES['RentingTypeImageModel']['name']['img_path'])) {
            $filePath = "/upload/renting_image_type/{$model->house_id}/";
            $fileName = $filePath . uniqid() . '.' . pathinfo($_FILES['RentingTypeImageModel']['name']['img_path'], PATHINFO_EXTENSION);
            $filePath = \Yii::getAlias('@webroot') . $filePath;
            if (!file_exists($filePath)) {
                mkdir($filePath, 0777, true);
            }
            $savePath = \Yii::getAlias('@webroot') . $fileName;
            if (move_uploaded_file($_FILES['RentingTypeImageModel']['tmp_name']['img_path'], $savePath)) {
                $model->img_path = $fileName;
            }
        }
    }
}